﻿using AppServiceDtoToEntityDemo.Application.Orders.Assemblers;
using AppServiceDtoToEntityDemo.Application.Orders.Dtos;
using AppServiceDtoToEntityDemo.Domain.Orders;

namespace AppServiceDtoToEntityDemo.Application.Orders;

public interface IOrderAppService
{
    int Create(CreateOrderDto input);
}

public class OrderAppService : IOrderAppService
{
    private readonly IOrderRepository _orderRepository;
    private readonly ICreateOrderDtoAssembler _createOrderDtoAssembler;

    public OrderAppService(ICreateOrderDtoAssembler createOrderDtoAssembler, IOrderRepository orderRepository)
    {
        _createOrderDtoAssembler = createOrderDtoAssembler;
        _orderRepository = orderRepository;
    }

    public int Create(CreateOrderDto input)
    {
        var order = _createOrderDtoAssembler.ComposeOrder(input);
        _orderRepository.Insert(order);
        return order.OrderItems.Count;
    }
}
